package pl.edu.mimuw.mas.bezier;

import java.io.Serializable;

import pl.edu.mimuw.mas.chart.Area;

/**
 * Reprezentacja siatki kontrolnej.
 */
public class ControlPoints implements Serializable {
	private static final long serialVersionUID = -2016212719380402197L;
	
	private boolean[][] activeNodes;
	private float[][] grid;	
	private float columns;
	private float rows;
	private Area area;
	
	/**
	 * Creates new uniform grid to model area.
	 * @param columns number of columns in control grid
	 * @param rows number of rows in control grid
	 * @param area array {x_1, y_1, x_2, y_2} describing position of modelled area
	 */
	public ControlPoints(int columns, int rows, Area area) {		
		this.grid = new float[columns][rows];
		this.activeNodes = new boolean[columns][rows];

		this.area = area;
		
		this.columns = columns;
		this.rows = rows;				
	}
	
	public void select(int col, int row) {
		activeNodes[col][row] = true;
	}
	
	public void unselect(int col, int row) {
		activeNodes[col][row] = false;
	}
	
	public void toggle(int col, int row) {
		activeNodes[col][row] = activeNodes[col][row] ? false : true;
	}
	
	public void selectNone() {
		for (int c = 0; c < columns; c++) {
			for (int r = 0; r < rows; r++) {
				activeNodes[c][r] = false;
			}
		}
	}

	public boolean isSelected(int col, int row) {
		return activeNodes[col][row];
	}
	
	public float getHeight(int col, int row) {
		return grid[col][row];
	}
	
	public void setHeight(int col, int row, float hgt) {
		grid[col][row] = hgt;
	}
	
	public void shiftSelectedHeight(float shift) {
		for (int i = 0; i < getColumnsCount(); i++) {
			for (int j = 0; j < getRowsCount(); j++) {
				if (activeNodes[i][j]) {
					grid[i][j] = grid[i][j] + shift;
				}
			}
		}
	}
	
	public Area getArea() {
		return area;
	}

	public float getColumnsCount() {
		return columns;
	}

	public float getRowsCount() {
		return rows;
	}
	
	public String getInfo() {
		return "Siatka - wiersze: " + rows + " kolumny: " + columns;
	}
}
